﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;

namespace LiMinCRMPlugin
{
    [Description("应收单 公用：保存时自动提交、审核")]
    [HotUpdate]
    public class ReceivableSaveServer : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("F_RDLV_Seasoning");
            e.FieldKeys.Add("FSetAccountType");
            e.FieldKeys.Add("FCURRENCYID");
        }

        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            foreach (DynamicObject entity in e.DataEntitys)
            {
                string FID = entity["ID"].ToString();

                string Seasoning = entity["F_RDLV_Seasoning"].ToString();

                string userName = this.Context.UserName;

                DynamicObject currecy = entity["CURRENCYID"] as DynamicObject;//币别 
                string currecyName = "";//
                if (currecy != null)
                {
                    currecyName = currecy["Name"].ToString();
                }

                string SetAccountType = entity["FSetAccountType"].ToString();
                
                if ((Seasoning == "1" || Seasoning == "True") && userName == "15212526750" && currecyName == "人民币" && SetAccountType=="3")
                {
                    ISubmitService submitService = ServiceHelper.GetService<ISubmitService>();
                    var submitResult = submitService.Submit(this.Context, this.BusinessInfo, new string[] { FID.ToString() }, "");

                    if (submitResult.IsSuccess)
                    {
                        OperateOption option = OperateOption.Create();
                        IAuditService auditService = ServiceHelper.GetService<IAuditService>();
                        var auditResult = auditService.Audit(this.Context, this.BusinessInfo, new string[] { FID.ToString() }, option);
                    }
                }


            }
        }
    }
}
